<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="Codeasy,Code Easy,SQL格式化,SQL转化字符串,去掉首尾引号,格式化代码,SQL转化为一行,java字符,提高开发效率,java,代码,开发工具,辅助开发">
<meta http-equiv="description" content="SQL转字符串工具">

<script type="text/javascript">
		
			 //对JS中endWith的扩展，判断某个字符串是否以某个字符串结尾
			 String.prototype.endWith=function(str){
			  if(str==null||str==""||this.length==0||str.length>this.length)
				 return false;
			  if(this.substring(this.length-str.length)==str)
				 return true;
			  else
				 return false;
			  return true;
			 }
			
			 //对JS中startWith的扩展，判断某个字符串是否以某个字符串开始
			 //用法：var value = "abcdefg"; value.startWith("abc"); //return true
			 String.prototype.startWith=function(str){
			  if(str==null||str==""||this.length==0||str.length>this.length)
			   return false;
			  if(this.substr(0,str.length)==str)
				 return true;
			  else
				 return false;
			  return true;
			 }
		 
			var userAgent = navigator.userAgent.toLowerCase();
			var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
			var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
			var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
		
		
			function f_copyCode(objName) {
				var obj = document.getElementsByName(objName)[0];
				if (obj == null || isUndefined(obj)) {
					obj = document.getElementById(objName);
				}
					
				if(is_ie && obj.style.display != 'none') {
					var rng = document.body.createTextRange();
					rng.moveToElementText(obj);
					rng.scrollIntoView();
					rng.select();
					rng.execCommand("Copy");
					rng.collapse(false);
				}else{
					alert("遗憾呐，当前操作只支持IE浏览器，请手动选择文本进行复制吧。");
				}
				 
			}
			
			function isUndefined(variable) {
			  return typeof variable == 'undefined' ? true : false;
		    }
		
		
		
			function trim(s)
			{
			  var strReturn;
			  strReturn=s;
			
			  while(strReturn.indexOf(" ")==0) strReturn=strReturn.substring(1);
			  if(strReturn.length==0) return "";
			  while(strReturn.lastIndexOf(" ")==strReturn.length-1)
			  {
				strReturn=strReturn.substring(0,strReturn.length-1);
				if(strReturn.length==0) return "";
			  }
			  return strReturn;
			}
		
		    var oldStr = "";
			function f_deal() {   
				var content = document.getElementById("content").value;
				oldStr = content;
				var valArray = new Array(); 
				valArray = content.split("\n");//按行分割字符串
				var longStr = "";
				
				for (var i = 0; i < valArray.length; i++) {
		
					var regR = /[\r]/g;
		            var regN = /[\n]/g;
					var regT = /[\s|\t]/g; //空格，制表符
		            
		            valArray[i] = valArray[i].replace(regR," ");
		            valArray[i] = valArray[i].replace(regN," ");
					valArray[i] = valArray[i].replace(regT," ");
		
		
				    longStr = longStr + " " + trim(valArray[i]);
			 
				}
				//alert(longStr);
				document.getElementById("newContent").value = longStr;
			}
			
			function f_addQuo () {
			
				var content = document.getElementById("content").value;
				oldStr = content;
				var valArray = new Array(); 
				valArray = content.split("\n");//按行分割字符串
				var longStr = "";
				
				for (var i = 0; i < valArray.length; i++) {
		
					var regR = /[\r]/g;
		            var regN = /[\n]/g;
					var regT = /[\s|\t]/g; //空格，制表符
		            
		            valArray[i] = valArray[i].replace(regR," ");
		            valArray[i] = valArray[i].replace(regN," ");
					valArray[i] = valArray[i].replace(regT," ");
		
		            if (!trim(valArray[i]) == "") {
					    longStr = longStr + " + \" " + trim(valArray[i]) + " \"\n";		
					}
					
		
				}
				
				longStr = "String sqlStr = \n  " + longStr.substr(2, longStr.length-3) + ";";
				//alert(longStr);
				document.getElementById("newContent").value = longStr;
			
			}
			
			//删除引号和加号
			function f_removeQuo() {
			 		var content = document.getElementById("content").value;
					oldStr = content;
					var valArray = new Array(); 
					valArray = content.split("\n");//按行分割字符串
					var longStr = "";
					
					for (var i = 0; i < valArray.length; i++) {
						valArray[i] = f_removeSign(valArray[i], "\"|+|;|\“|\”|；");
						
						if (i+1 == valArray.length) {
							valArray[i] = valArray[i] + ";";
						} else {
							valArray[i] = valArray[i] + "\n";
						}
						
						longStr = longStr +  valArray[i] ;
						
						
					}
					//alert(longStr);
					document.getElementById("newContent").value = longStr;
				
			}
			
			//移除头尾的符号
			function f_removeSign(str, signStr) {
			    var signArray = signStr.split("|");
				var newStr = trim(str);
				
				var regR = /[\r]/g; //回车
		        var regN = /[\n]/g; //换行
		        var regT = /[\s|\t]/g; //空格，制表符
				    
		        newStr = newStr.replace(regR," ");
		        newStr = newStr.replace(regN," ");
				newStr = newStr.replace(regT," "); 
		        
			 
		        newStr = trim(newStr);
				var i = 0;
				//先处理头部
				for (; i < signArray.length; i++) {  
					var sign = signArray[i];
					if (newStr.startWith(sign)) {
						newStr = trim(newStr.substring(1));
						i = -1;
					} 
				}
		 	
				i = 0;
				//再处理尾部
				for (; i < signArray.length; i++) {  
					var sign = signArray[i];
					if (newStr.endWith(sign)) {
						newStr = trim(newStr.substring(0,newStr.length-1));
						i = -1;
					} 
				}
				
				
			
				return newStr;
			}
			
			function f_reset() {
				
			   document.getElementById("content").value = "";
			   document.getElementById("newContent").value = "";
			}
		 
		</script>

<style type="text/css">
table {
	line-height: 30px;
	margin-left: 100px;
}

.page_head {
	margin-left: 30px;
}
</style>
<title>SQL和字符串互转 - Codeasy</title>
</head>

<body>
<s:include value="/common/header.jsp"></s:include>
<table class="c_t">
	<tr>
		<td colspan="2" style="text-align: center;">
		<h2>SQL和字符串互转<a href="#"
			style="font-size: 12px; margin-left: 10px;">(使用帮助)</a></h2>
		</td>
	</tr>
	<tr>
		<td>源SQL</td>
		<td><textarea id="content" name="content" cols="130" rows="8"
			style="word-wrap: normal; overflow: scroll;" onfocus="f_selAll(this);"></textarea></td>
	</tr>
	<tr>
		<td>输出框</td>
		<td><textarea id="newContent" name="newContent" cols="130"
			rows="11" style="word-wrap: normal; overflow: scroll;" onfocus="f_selAll(this);"></textarea></td>
	</tr>
	<tr>
		<td colspan="2" style="text-align: center;"><input type="button"
			value="格式化为一行" onclick="f_deal();" /> <input type="button"
			value="拼成字符串" onclick="f_addQuo();" /> <input type="button"
			value="还原为SQL" onclick="f_removeQuo();" /> <input type="button"
			value="复制" onclick="f_copyCode('newContent');" /> <input
			type="button" value="清空" onclick="f_reset();" /></td>

	</tr>
</table>

<s:include value="/common/footer.jsp"></s:include>
</body>
</html>
